package com.itouch.bracelet.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itouch.bracelet.R;
import com.itouch.bracelet.bean.NearbyPeople;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * cocoaSJ
 */
public class NearbyPeopleAdapter extends BaseAdapter 
		 {
	private ArrayList<NearbyPeople> list;
	private Context context;
	private ImageLoader  imageLoader = ImageLoader.getInstance();
	private LayoutInflater infalter;
	private DisplayImageOptions options;
	
	
	public NearbyPeopleAdapter(Context context, ArrayList<NearbyPeople> list) {
		this.list = list;
		this.context = context;
		  infalter = LayoutInflater.from(context);
		  options = new DisplayImageOptions.Builder()
			.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
			.showImageOnLoading(R.drawable.default_face)
			.showImageForEmptyUri(R.drawable.default_face)
			
			.displayer(new RoundedBitmapDisplayer(5))
			.showImageOnFail(R.drawable.default_face).cacheInMemory(true)
			.cacheOnDisc(true).considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public NearbyPeople getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View converView, ViewGroup viewGrop) {
		// TODO Auto-generated method stub
		ViewHolder vh = null;
		if (converView == null) {
			vh = new ViewHolder();
			converView =infalter.inflate(
					R.layout.adapter_item_nearby, viewGrop,false);
			vh.send = (TextView) converView.findViewById(R.id.send);
			 vh.dis = (TextView) converView.findViewById(R.id.dis);
			 vh.name = (TextView) converView.findViewById(R.id.name);
			 vh.head = (ImageView) converView.findViewById(R.id.head);
			converView.setTag(vh);
		} else {
			vh = (ViewHolder) converView.getTag();
		}
		NearbyPeople bean = getItem(position);
		if("0".equals(bean.getNum())){
			vh.send .setClickable(true);
			vh.send .setText(R.string.add);
			vh.send .setTextColor(Color.parseColor("#ef6562"));
			vh.send.setBackgroundResource(R.drawable.add_btn);
		} else {
			vh.send .setClickable(false);
			vh.send .setText(R.string.added);
			vh.send .setTextColor(Color.parseColor("#cdcdcd"));
			vh.send.setBackgroundColor(Color.parseColor("#00000000"));
		}
		
		vh.send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				listener.add(position);
			}
		});
		
		
		vh.dis.setText(bean.getDistance());
		vh.name.setText(bean.getName());
		imageLoader.displayImage(bean.getPath(), vh.head,
				options);
		
		return converView;
	}

	private AddLinstener listener;

	public void setListener(AddLinstener listener) {
		this.listener = listener;
	}



	public interface AddLinstener{
		
		public void add(int position);
	}
	
	

	class ViewHolder {
		public ImageView head;
		public TextView name;
		public TextView dis;
		public TextView send;
	}

}
